using System;
using System.Collections.Generic;
using System.Text;
using DTO;
using DAO;
using System.Data;
using System.Text.RegularExpressions;

namespace BUS
{
    /// <summary> 
    ///Author: nnamthach@gmail.com 
    /// <summary>
    public class BudgetsControl
    {
        #region Local Variable
        private BudgetsDataAccess _BudgetsDAO;
        #endregion Local Variable

        #region Method
        public BudgetsControl()
        {
            _BudgetsDAO = new BudgetsDataAccess();
        }

        public BudgetsInfo Get_Budgets(
            String Contract,
            Int32 aYear
        , ref string sErr)
        {
            return _BudgetsDAO.Get_Budgets(
            Contract,
            aYear
            , ref sErr);
        }

        public void SetTransaction(object trans)
        {
            _BudgetsDAO.SetTransaction(trans);
        }

        public DataTable GetAll_Budgets(ref string sErr)
        {
            return _BudgetsDAO.GetAll_Budgets(ref sErr);
        }


        public DataTable GetAll_Budgets1(ref string sErr)
        {
            return _BudgetsDAO.GetAll_Budgets1(ref sErr);
        }

        public DataTable GetAll_bgGroup(ref string sErr)
        {
            return _BudgetsDAO.GetAll_bgGroup(ref sErr);
        }

        public DataTable GetAll_bgGroup1(ref string sErr)
        {
            return _BudgetsDAO.GetAll_bgGroup1(ref sErr);
        }

        public DataTable GetCond(String strCond, ref string sErr)
        {
            return _BudgetsDAO.GetCond(strCond, ref sErr);
        }

        public DataTable GetCond1(String strCond, ref string sErr)
        {
            return _BudgetsDAO.GetCond1(strCond, ref sErr);
        }

        public DataTable GetTotal(String strCond, ref string sErr)
        {
            return _BudgetsDAO.GetTotal(strCond, ref sErr);
        }

        public DataTable GetTotal1(String strCond, ref string sErr)
        {
            return _BudgetsDAO.GetTotal1(strCond, ref sErr);
        }

        public Int32 Add_Budgets(BudgetsInfo obj, ref string sErr)
        {
            return _BudgetsDAO.Add_Budgets(obj, ref sErr);
        }

        private BudgetsInfo CalcSort(BudgetsInfo obj)
        {
            if (obj.Contract.Length > obj.BudgetGroup.Length + 1)
            {
                obj.sort1 = obj.Contract.Substring(0, obj.BudgetGroup.Length + 1);
                string tmp = obj.Contract.Substring(obj.BudgetGroup.Length + 1, obj.Contract.Length - obj.BudgetGroup.Length - 1);
                if (Regex.IsMatch(tmp, "^[0-9]+$"))
                {
                    obj.sort2 = Convert.ToDouble(tmp);
                }
            }
            else
            {
                obj.sort1 = obj.Contract;
                obj.sort2 = 1;
            }
            return obj;

        }
        public Int32 Add_Budgets1(BudgetsInfo obj, ref string sErr)
        {
            return _BudgetsDAO.Add_Budgets1(obj, ref sErr);
        }

        public string Update_Budgets(BudgetsInfo obj)
        {
            return _BudgetsDAO.Update_Budgets(obj);
        }
        public string Update_Budgets1(BudgetsInfo obj)
        {
            return _BudgetsDAO.Update_Budgets1(obj);
        }

        public string Update_Shared(BudgetsInfo obj)
        {
            return _BudgetsDAO.Update_Shared(obj);
        }

        public Boolean IsExist_GroupOtherShared(
            String Contract,
            Int32 aYear,
            String BudgetGroup,
            Int16 SharedBudget
        )
        {
            return _BudgetsDAO.IsExist_GroupOtherShared(Contract, aYear, BudgetGroup, SharedBudget);
        }

        public Boolean IsExist_GroupOtherShared1(
            String Contract,
            Int32 aYear,
            String BudgetGroup,
            Int16 SharedBudget
        )
        {
            return _BudgetsDAO.IsExist_GroupOtherShared1(Contract, aYear, BudgetGroup, SharedBudget);
        }

        public string Delete_Budgets(
            String Contract,
            Int32 aYear
        )
        {
            return _BudgetsDAO.Delete_Budgets(
            Contract,
            aYear
            );
        }
        public string Delete_Budgets1(
            String Contract,
            Int32 aYear
        )
        {
            return _BudgetsDAO.Delete_Budgets1(
            Contract,
            aYear
            );
        }
        public Boolean IsExist_Budgets(
            String Contract,
            Int32 aYear
        )
        {
            return _BudgetsDAO.IsExist_Budgets(
            Contract,
            aYear
            );
        }
        public Boolean IsExist_Budgets1(
            String Contract,
            Int32 aYear
        )
        {
            return _BudgetsDAO.IsExist_Budgets1(
            Contract,
            aYear
            );
        }

        /*  public string Delete_Contr(String arrID)
          {
              string kq = "";
              string[] arrStrID = arrID.Split(',');
              foreach (string strID in arrStrID)
              {
                  if (strID != "")
                  {
                      int ID = Convert.ToInt32(strID);
                      kq += _ContrDAO.Delete_Contr(ID);
                  }
              }
              return kq;
          } */



        public DataTable Search(String columnName, String columnValue, String condition, ref String sErr)
        {
            return _BudgetsDAO.Search(columnName, columnValue, condition, ref  sErr);
        }

        public string InsertUpdate(BudgetsInfo obj)
        {
            string sErr = "";
            if (IsExist_Budgets(
            obj.Contract, obj.aYear
            ))
            {
                sErr = Update_Budgets(obj);
            }
            else
                Add_Budgets(obj, ref sErr);
            return sErr;
        }

        public string InsertUpdate1(BudgetsInfo obj)
        {
            string sErr = "";
            if (IsExist_Budgets1(
            obj.Contract, obj.aYear
            ))
            {
                sErr = Update_Budgets1(obj);
            }
            else
                Add_Budgets1(obj, ref sErr);
            return sErr;
        }

        public string TransferIn(DataRow row)
        {
            BudgetsInfo inf = new BudgetsInfo(row);
            return InsertUpdate(inf);
        }

        public string TransferIn1(DataRow row)
        {
            BudgetsInfo inf = new BudgetsInfo(row);
            return InsertUpdate1(inf);
        }

        public decimal Budgets_getSumAmt(String Contract, Int32 aYear, ref string sErr)
        {
            return _BudgetsDAO.Budgets_getSumAmt(Contract, aYear, ref sErr);
        }

        public decimal Budgets_getSumAmt1(String Contract, Int32 aYear, ref string sErr)
        {
            return _BudgetsDAO.Budgets_getSumAmt1(Contract, aYear, ref sErr);
        }

        public DataTable Budgets_getBudgets(
            String Contract,
            Int32 aYear, ref string sErr
        )
        {
            return _BudgetsDAO.Budgets_getBudgets(
            Contract,
            aYear, ref sErr
            );
        }

        public DataTable Budgets_getBudgets1(
            String Contract,
            Int32 aYear, ref string sErr
        )
        {
            return _BudgetsDAO.Budgets_getBudgets1(
            Contract,
            aYear, ref sErr
            );
        }

        #endregion Method


        public string updateTotal(string budId, string code, int year, ref string sErr)
        {
            if (budId == "HL") return _BudgetsDAO.updateTotalHL(code, year, ref sErr);
            else return _BudgetsDAO.updateTotalHV(code, year, ref sErr);
        }

        public decimal getTotalAmt(string busId, string code, int year, ref string sErr)
        {
            if (busId == "HL") return _BudgetsDAO.getTotalAmtHL(code, year, ref  sErr);
            return _BudgetsDAO.getTotalAmtHV(code, year, ref  sErr);
        }

        public DataTable getTotalAmts(string busId, string code, int year, ref string sErr)
        {
            if (busId == "HL") return _BudgetsDAO.getTotalAmtsHL(code, year, ref  sErr);
            return _BudgetsDAO.getTotalAmtsHV(code, year, ref  sErr);
        }
        public DataTable GetGroup(string busId, ref string sErr)
        {
            if (busId == "HL") return GetAll_bgGroup(ref sErr);
            return GetAll_bgGroup1(ref sErr);
        }
        public DataTable GetAll(string busId, ref string sErr)
        {
            if (busId == "HL") return _BudgetsDAO.GetAll_Budgets(ref sErr);
            return _BudgetsDAO.GetAll_Budgets1(ref sErr);
        }

        public BudgetsInfo Get_Budget(string busId, string code, int year, ref string sErr)
        {
            if (busId == "HL") return _BudgetsDAO.Get_BudgetsHL(code, year, ref sErr);
            return _BudgetsDAO.Get_BudgetsHV(code, year, ref sErr);
        }

        public DataTable GetAll(string busId, int year, ref string sErr)
        {
            if (busId == "HL") return _BudgetsDAO.GetAllYearHL(year, ref sErr);
            return _BudgetsDAO.GetAllYearHV(year, ref sErr);
        }

        public object GetShortAll(string busId, ref string sErr)
        {
            if (busId == "HL") return _BudgetsDAO.GetShortAllHL(ref sErr);
            return _BudgetsDAO.GetShortAllHV(ref sErr);
        }

        public bool IsExist(string busId, string contract, int year)
        {
            if (busId == "HL") return IsExist_Budgets(contract, year);
            return IsExist_Budgets1(contract, year);
        }

        public string Update(string busId, BudgetsInfo info)
        {
            info = CalcSort(info);
            if (busId == "HL") return Update_Budgets(info);
            return Update_Budgets1(info);
        }

        public void Add(string busId, BudgetsInfo info, ref string sErr)
        {
            info = CalcSort(info);
            if (busId == "HL") Add_Budgets(info, ref sErr);
            else Add_Budgets1(info, ref sErr);
        }

        public bool IsExistGroupOtherShared(string busId, string contract, int year, string group, short shared)
        {
            if (busId == "HL") return IsExist_GroupOtherShared(contract, year, group, shared);
            return IsExist_GroupOtherShared1(contract, year, group, shared);
        }

        public string Delete(string busId, string contract, int year)
        {
            if (busId == "HL") return Delete_Budgets(contract, year);
            return Delete_Budgets1(contract, year);
        }

        public DataTable GetCond(string busId, string strFilter, ref string sErr)
        {
            if (busId == "HL") return GetCond(strFilter, ref sErr);
            return GetCond1(strFilter, ref sErr);
        }

        public decimal getSumAmt(string busId, string code, int year, ref string sErr)
        {
            if (busId == "HL") return Budgets_getSumAmt(code, year, ref sErr);
            return Budgets_getSumAmt1(code, year, ref sErr);
        }
        public object BeginTransaction(ref string sErr)
        {
            return _BudgetsDAO.BeginTransaction(ref sErr);
        }

        public bool CommitTransaction(ref string sErr)
        {
            return _BudgetsDAO.CommitTransaction(ref sErr);
        }

        public bool RollbackTransaction(ref string sErr)
        {
            return _BudgetsDAO.RollbackTransaction(ref sErr);
        }

        public decimal getTotalGroupAmt(string busId, string code, int year, ref string sErr)
        {
            if (busId == "HL") return _BudgetsDAO.getTotalGroupAmtHL(code, year, ref sErr);
            return _BudgetsDAO.getTotalGroupAmtHV(code, year, ref sErr);
        }
    }
}
